package com.learn.core.controller.learn;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.learn.common.annotation.UserLoginToken;
import com.learn.common.enums.ResultEnum;
import com.learn.common.utils.DateUtils;
import com.learn.common.utils.JwtUtil;
import com.learn.common.utils.ResultUtil;
import com.learn.common.utils.StringUtils;
import com.learn.common.utils.result.CommonResult;
import com.learn.core.domain.*;
import com.learn.core.domain.view.ExamUser;
import com.learn.core.domain.view.ExamineUser;
import com.learn.core.service.IExamineUserService;
import com.learn.core.service.ILExamineQuestionService;
import com.learn.core.service.ILExamineService;
import com.learn.core.service.ILExamineUserService;
import com.learn.framework.web.controller.WebController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Description
 *
 * @author zhaohongyu
 * @ClassName ExamineController
 * @date 2021.04.26 16:48
 * @describe 问卷调查
 */
@Api("问卷调查")
@RestController
@RequestMapping("/learn/examins")
public class ExamineController extends WebController<LExamine> {

    @Autowired
    private ILExamineService ilExamineService;
    @Autowired
    private IExamineUserService iExamineUserService;
    @Autowired
    private ILExamineUserService ilExamineUserService;
    @Autowired
    private ILExamineQuestionService ilExamineQuestionService;

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    //获取问卷调查列表
    @ApiOperation(value = "获取问卷调查列表")
    @UserLoginToken
    @PostMapping("/list")
    public CommonResult<Object> list(){
        try {
            Date date = new Date();
            String startTime = simpleDateFormat.format(date);
            QueryWrapper<LExamine> queryWrapper = new QueryWrapper<LExamine>();
            queryWrapper.eq("examine_state","1");
            queryWrapper.apply("start_time <= '"+startTime +"' AND end_time >= '"+startTime +"'");
            queryWrapper.orderByAsc("create_time");
            List<LExamine> lExamines = ilExamineService.list(queryWrapper);
            // 返回
            return (CommonResult) ResultUtil.success(ResultEnum.SUCCESS,lExamines);
        }catch (Exception e){
            e.printStackTrace();
            return (CommonResult) ResultUtil.error(ResultEnum.ERROR);
        }
    }

    //近期即将开始列表
    @ApiOperation(value = "近期即将开始列表")
    @UserLoginToken
    @PostMapping("/aboutExamine")
    public CommonResult<Object> aboutExamine(){
        try {
            String startTime = DateUtils.tomorrowDateStr() + " 00:00:00";
            String endTime = DateUtils.tomorrowDateStr() + " 23:59:59";
            QueryWrapper<LExamine> queryWrapper = new QueryWrapper<LExamine>();
            queryWrapper.eq("examine_state","1");
            queryWrapper.ge("start_time",startTime);
            queryWrapper.le("end_time",endTime);
            queryWrapper.orderByAsc("create_time");
            List<LExamine> list = ilExamineService.list(queryWrapper);

            return (CommonResult) ResultUtil.success(ResultEnum.SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
            return (CommonResult) ResultUtil.error(ResultEnum.ERROR);
        }
    }

    //问卷调查排行
    @ApiOperation(value = "问卷调查排行")
    @UserLoginToken
    @PostMapping("/rankingExamine")
    public CommonResult<Object> rankingExamine(){
        try {
            QueryWrapper<ExamineUser> queryWrapper = new QueryWrapper<ExamineUser>();
            queryWrapper.select("examine_id,count(*) as examineCount");
            queryWrapper.groupBy("examine_id");
            queryWrapper.orderByDesc("examineCount");
            queryWrapper.last("limit 0,10");
            List<ExamineUser> examineUsers = iExamineUserService.list(queryWrapper);
            List<Map<String,Object>> list = new ArrayList<>();
            for (ExamineUser examineUser : examineUsers) {
                Map<String,Object> map = new HashMap<>();
                map.put("id",examineUser.getExamineId());
                LExamine examine = ilExamineService.getById(examineUser.getExamineId());
                if(null != examine)
                    map.put("name",examine.getExamineName());
                map.put("examCount",examineUser.getExamineCount());
                list.add(map);
            }
            return (CommonResult) ResultUtil.success(ResultEnum.SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
            return (CommonResult) ResultUtil.error(ResultEnum.ERROR);
        }
    }

    //问卷调查详情
    @ApiOperation(value = "问卷调查详情")
    @UserLoginToken
    @GetMapping("/examineInfo")
    public CommonResult<Object> examineInfo(@RequestParam("examineId")Integer
                                                        examineId, HttpServletRequest request){
        try {
            LExamine lExamine = ilExamineService.getById(examineId);
            QueryWrapper<LExamineUser> queryWrapper = new QueryWrapper<LExamineUser>();
            queryWrapper.eq("examine_id",lExamine.getExamineId());
            LExamineUser lExamineUser = ilExamineUserService.getOne(queryWrapper);
            lExamine.setlExamineUser(lExamineUser);
            QueryWrapper<LExamineQuestion> questionQueryWrapper = new QueryWrapper<LExamineQuestion>();
            questionQueryWrapper.eq("examine_id",lExamine.getExamineId());
            List<LExamineQuestion> lExamineQuestions = ilExamineQuestionService.list(questionQueryWrapper);
            lExamine.setlExamineQuestions(lExamineQuestions);
            return (CommonResult) ResultUtil.success(ResultEnum.SUCCESS,lExamine);
        }catch (Exception e){
            e.printStackTrace();
            return (CommonResult) ResultUtil.error(ResultEnum.ERROR);
        }
    }

    //个人作答情况
    @ApiOperation(value = "个人作答情况")
    @UserLoginToken
    @PostMapping("/examineUser")
    public CommonResult<Object> examineUser(@RequestBody LExamineUser lExamineUser, HttpServletRequest request){
        String  token = request.getHeader("token");
        String userId = JwtUtil.getUserId(token);
        try {
            if(StringUtils.isNotEmpty(lExamineUser.getExamineId())){
                lExamineUser.setCreateTime(new Date());
                lExamineUser.setCreateUser(userId);
                lExamineUser.setState("1");
                if(ilExamineUserService.save(lExamineUser))
                    return (CommonResult) ResultUtil.success(ResultEnum.SUCCESS);
            }
        }catch (Exception e){
            e.printStackTrace();
            return (CommonResult) ResultUtil.error(ResultEnum.ERROR);
        }
        return (CommonResult) ResultUtil.error(ResultEnum.ERROR);
    }
}
